<?php
namespace app\index\controller;
//导入Controller
use think\Controller;

//导入数据库
use think\Db;
use think\Session;
class Orderinfo extends Allow{
	public function getindex(){
		//通过session中的用户id来查询单号总价和状态
		
		$brand=Db::table("brand")->select();
		$links=Db::table("links")->select();
		$userid=Session::get('uid');
		$info=Db::table("order")->field("ordernum,status,allprice,time,id")->where("user_id",$userid)->select();
		// var_dump($info);
		
		//三表联合查询 order别名 o / order_shop 别名 s /goods 别名 g   查询order中的id与order_info的id和 order_info表中的goodsid 
		// $list=Db::table('order')->alias('o')->join('order_info i','o.id=i.order_id')->join('pro_goods g','i.goods_id=g.id');
		$list=Db::query("SELECT * FROM `order` o,`order_info` oi,`pro_goods` pg WHERE o.id=oi.order_id and oi.goods_id=pg.id");
		// print_r($list);
		if(!empty($list)){
			//如果$list不为空  以订单数量循环
			for($i=0;$i<count($info);$i++){
				//遍历$list
				foreach($list as $key=>$val){
					if($info[$i]['ordernum']==$val['ordernum']){
						$info[$i]['pp'][] = $val;
					}
				}
			}
		}

		// 查询状态为0
		$filter = [0];

		// 存储查找结果
		$result = [];

		// 对数组进行查询
		array_map(function ($row) use ($filter, &$result) {
    		if(in_array($row['status'], $filter)) {
      		$result[] = $row;
    	}
	}, $info);



		//查询状态为1
		$filter1= [1];

		// 存储查找结果
		$result1= [];

		// 对数组进行查询
		array_map(function ($row) use ($filter1, &$result1) {
    		if(in_array($row['status'], $filter1)) {
      		$result1[] = $row;
    	}
	}, $info);


		//查询状态为2
		$filter2= [2];

		//存储查找结果
		$result2=[];

		//对数组进行查询
		array_map(function ($row) use ($filter2, &$result2) {
    		if(in_array($row['status'], $filter2)) {
      		$result2[] = $row;
    	}
	}, $info);

		// var_dump($info[0]);


		//加载订单详情模版
		return $this->fetch("Orderinfo/orderinfo",['info'=>$info,'result'=>$result,'result1'=>$result1,'result2'=>$result2,'brand'=>$brand,'links'=>$links]);
		
	}

	public function getqueren(){
		$id=$_GET['a'];
		// echo($id);
		$bool=Db::table("order")->where('id', $id)->update(['status'=>'2']);
		if($bool){
			echo 1;
		}else{
			echo 2;
		}
	}

	public function getquxiao(){
		$id=$_GET['a'];
		$bool=Db::table("order")->where('id', $id)->update(['status'=>'3']);
		if($bool){
			echo 1;
		}else{
			echo 2;
		}
	}

	public function gettxfahuo(){
		$id=$_GET['a'];
		$bool=Db::table("order")->where('id', $id)->update(['status'=>'5']);
		if($bool){
			echo 1;
		}else{
			echo 2;
		}
	}
	//订单详情地址
	public function getaddress(){
		//获得订单号 以订单号获取 编号 时间 id 状态
		$brand=Db::table("brand")->select();
		$links=Db::table("links")->select();
		$request=request();
		$id=$request->param('id');
		// echo $id;
		$data=Db::table('order')->where('id',$id)->select();

		$list=Db::query("SELECT * FROM `order` o,`order_info` oi,`pro_goods` pg WHERE o.id=oi.order_id and oi.goods_id=pg.id");
		if(!empty($list)){
			//如果$list不为空  以订单数量循环
			for($i=0;$i<count($data);$i++){
				//遍历$list
				foreach($list as $key=>$val){
					if($data[$i]['ordernum']==$val['ordernum']){
						$data[$i]['pp'][] = $val;
					}
				}
			}
			// var_dump($data[0]['pp']);
		}
		// var_dump($list);
	   	return $this->fetch("Orderinfo/oaddress",['data'=>$data[0],'pp'=>$data[0]['pp'],'brand'=>$brand,'links'=>$links]);
	}

	//个人地址
	public function getaddressa(){
		$brand=Db::table("brand")->select();
		$links=Db::table("links")->select();

		//获取地址信息
 		$address=Db::table("address")->where("user_id",Session::get('uid'))->select();



		return $this->fetch("Orderinfo/dz",['brand'=>$brand,'links'=>$links,'address'=>$address]);
	}

	public function gettwo(){
		$request=request();
       //获取商品id
        $upid=$request->param('upid');
	 //基本操作
		// $sql="SELECT * FROM district WHERE upid={$_GET['upid']}";

	 //获取所有数据
	   $list=Db::table("district")->where("upid","{$upid}")->select();
		echo json_encode($list);
	}

	public function postinsertaddress(){
		$request=request();
		$data=$request->only(['name','phone','huo','adds']);
		$data['user_id']=Session::get('uid');
		//执行插入
		Db::table("address")->insert($data);
		 $this->success("添加地址成功","/orderinfo/addressa");
	}
	//地址删除
	public function getdeladdr(){
		$id=$_GET['id'];
		$bool=Db::table('address')->where('id',$id)->delete();
		if($bool){
			echo 1;
		}else{
			echo 2;
		}
	}

	//个人信息
	public function getgrxx(){
		$brand=Db::table("brand")->select();
		$links=Db::table("links")->select();

		//查询用户信息
		$id=Session::get('uid');
		$info=Db::table('member')->where('id',$id)->select();
		// var_dump($info);

		return $this->fetch("Orderinfo/grxx",['brand'=>$brand,'links'=>$links,'info'=>$info[0]]);
	}

	//修改个人信息
	public function postupdate(){
		$request=request();
		$id=Session::get('uid');
		//获取修改数据

		$data=$request->only(['email','phone','name','nickname','sex','birthday',]);
		// var_dump($data);
		 if(Db::table('member')->where('id',"{$id}")->update($data)){
            $this->success("修改成功","/orderinfo/grxx");
        }else{
            $this->error("修改失败","/orderinfo/grxx");
        }
	}


//写评价
	public function getdoevaluate(){
		//品牌遍历
    	$brand=Db::table("brand")->select();
		//友情链接遍历
		$request=request();
		$id=$request->param('orderid');
		$links=Db::query("select linksname,linksaddress from links where status=0");

		$data=Db::table('order')->where('id',$id)->select();

		$list=Db::query("SELECT * FROM `order` o,`order_info` oi,`pro_goods` pg WHERE o.id=oi.order_id and oi.goods_id=pg.id");
		if(!empty($list)){
			//如果$list不为空  以订单数量循环
			for($i=0;$i<count($data);$i++){
				//遍历$list
				foreach($list as $key=>$val){
					if($data[$i]['ordernum']==$val['ordernum']){
						$data[$i]['pp'][] = $val;
					}
				}
			}
			// var_dump($data);exit;
		}
		//写评价
		return $this->fetch("Orderinfo/doevaluate",['brand'=>$brand,'links'=>$links,'data'=>$data[0]['pp']]);
	}
//处理评价
	public function postaddevaluate(){
		$time=time();
		$user_id=$_POST['user_id'];

		$ordernum=$_POST['ordernum'];
		$orderid=Db::query("select id from `order` where ordernum={$ordernum}")[0]['id'];

		$data=Db::table('order')->where('id',$orderid)->select();
		$list=Db::query("SELECT * FROM `order` o,`order_info` oi,`pro_goods` pg WHERE o.id=oi.order_id and oi.goods_id=pg.id");
		if(!empty($list)){
			//如果$list不为空  以订单数量循环
			for($i=0;$i<count($data);$i++){
				//遍历$list
				foreach($list as $key=>$val){
					if($data[$i]['ordernum']==$val['ordernum']){
						$data[$i]['pp'][] = $val;
					}
				}
			}
			foreach($data[0]['pp'] as $v){
				$goodsid=$v['id'];
				$content=$_POST['content'.$goodsid];
				$result=Db::execute("INSERT INTO `evaluate` VALUES(NULL,{$user_id},{$goodsid},{$content},NULL,0,{$time})");
			}
				if($result){
					$bool=Db::table("order")->where('id', $orderid)->update(['status'=>'7']);
					// var_dump($result);exit;
					$this->success("评价成功",'/orderinfo/evaluate');
				}
		
		}else{
			$this->error("评价失败,请重新评价");
		}
				// Db::execute("INSERT INTO `evaluate` VALUES(NULL,4,53,'666',NULL,0,1528686671)");
	}
//评价列表
	public function getevaluate(){
		//品牌遍历
    	$brand=Db::table("brand")->select();
    	// var_dump($brand);exit;
		//友情链接遍历
		$links=Db::query("select linksname,linksaddress from links where status=0");

		 //获取评价信息
		$request=request();
		$uid=Session::get('uid');
		// $uid=$request->param('uid');
		// var_dump($uid);exit;
        $eva=Db::query("SELECT pro.pic,pro.name,ev.content,ev.reply FROM `evaluate` ev,`pro_goods` pro where ev.uid={$uid} and ev.goods_id=pro.id and ev.status=0");
		// var_dump($eva);exit;

		//加载评价列表
		return $this->fetch("Orderinfo/evaluate",['eva'=>$eva,'brand'=>$brand,'links'=>$links]);
	}
//收藏列表
	public function getcollection(){
		//品牌遍历
    	$brand=Db::table("brand")->select();
    	// var_dump($brand);exit;
		//友情链接遍历
		$links=Db::query("select linksname,linksaddress from links where status=0");
		$uid=Session::get('uid');
		$list=Db::query("SELECT co.id,co.goods_id,pg.name,pg.price,pg.pic FROM  `collection` co,`pro_goods` pg WHERE co.user_id={$uid} and co.goods_id=pg.id");
		// var_dump($list);exit;

		//加载收藏列表
		return $this->fetch("Orderinfo/collection",['list'=>$list,'brand'=>$brand,'links'=>$links]);
	}
//移出收藏夹
	public function getremove(){
		//获取收藏信息
		$request=request();
		$id=$request->param('id');
		// $result=Db::exec("DELETE FROM `collection` WHERE id={$id}");
		$result=Db::table("collection")->where('id',$id)->delete();
		// var_dump($result);exit;
		if($result){
			$this->redirect("/orderinfo/collection");
		}else{
			$this->redirect("/orderinfo/collection");
		}
	}
//添加到收藏夹
	public function getaddcol(){
		$request=request();
		$goods_id=$request->param('id');
		$user_id=Session::get('uid');
		$select=Db::query("select * from collection where goods_id={$goods_id} and user_id={$user_id}");
		if($select){
			$this->error("请勿重复添加商品到收藏夹");
		}else{
			if(Db::table("collection")->insert(["goods_id"=>$goods_id,"user_id"=>$user_id])){
				$this->success("添加成功,请查看","/orderinfo/collection");
			}else{
				$this->error("添加失败,请重试");
			}
		}
		// var_dump($select);exit;

	}
}